#define DEBUG
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn=1000000;

class Splay {
public:
  int n, val[maxn+1], fa[maxn+1], son[maxn+1][2], delstack[maxn+1], cnt[maxn+1], size[maxn+1], root;

  void update(int o) {
    if (o) {
      size[o] = cnt[o]+size[son[o][0]]+size[son[o][1]];
    }
  }

  int sonPos(int x) {
    return son[fa[x]][0]==x ? 0 : 1;
  }

  void rotate(int x) {
    int y=fa[x], z=fa[y], t=sonPos(x);
    son[y][t] = son[x][!t];
    fa[son[x][!t]] = y;
    son[x][!t] = y;
    son[z][sonPos(fa[x])] = x;
    fa[y] = x;
    fa[x] = z;
    update(x);
    update(y);
    update(z);
  }

  int splay(int x, int y) {
    if (x) {
      for (; fa[x]!=y; ) {
	if (fa[fa[x]]!=y && sonPos(fa[x])==sonPos(x)) {
	  rotate(fa[x]);
	}
	rotate(x);
      }
      if (y==0) {
	root=x;
      }
    }
    return x;
  }

  int addNode(int f, int sonv, int x) {
    n++;
    cnt[n] = size[n] = 1;
    val[n] = sonv;
    fa[n] = f;
    son[f][x] = n;
    update(f);
    return n;
  }

  int ins(int o, int x) {
    if (!n) {
      return root=addNode(0, x, 0);
    }
    int temp;
    if (val[o]==x) {
      cnt[o]++;
      update(o), update(fa[o]);
      return o;
    } else if (val[o]>x) {
      if (son[o][0]) {
	temp=ins(son[o][0], x);
      } else {
	temp=addNode(o, x, 0);
      }
    } else {
      if (son[o][1]) {
	temp=ins(son[o][1], x);
      } else {
	temp=addNode(o, x, 1);
      }
    }
    update(o);
    return temp;
  }

  int find(int o, int x) {
    if (val[o]==x) {
      return o;
    } else if (x<val[o]) {
      return find(son[o][0], x);
    } else {
      return find(son[o][1], x);
    }
  }

  int getRank(int x) {
    return size[son[splay(x, 0)][0]]+1;
  }

  int getRankX(int o, int x) {
    if (size[son[o][0]]>=x) {
      return getRankX(son[o][0], x);
    } else if (size[son[o][0]]+cnt[o]>=x) {
      return splay(o, 0);
    } else {
      return getRankX(son[o][1], x-size[son[o][0]]-cnt[o]);
    }
  }
  
  int getMax(int o) {
    if (son[o][1]) {
      return getMax(son[o][1]);
    } else {
      return o;
    }
  }

  int getMin(int o) {
    if (son[o][0]) {
      return getMin(son[o][0]);
    } else {
      return o;
    }
  }

  int getPre(int x) {
    splay(x, 0);
    return son[x][0] ? getMax(son[x][0]) : 0;
  }

  int getSuc(int x) {
    splay(x, 0);
    return son[x][1] ? getMin(son[x][1]) : 0;
  }

  void del(int x) {
    splay(x, 0);
    cnt[x]--;
    update(fa[x]);
    if (!cnt[x]) {
      int a=getPre(x), b=getSuc(x);
      if (a) {
	splay(a, 0);
      }
      if (b) {
	splay(b, a);
      }
      son[fa[x]][sonPos(x)] = 0;
      size[x]=cnt[x]=val[x]=0;
      update(fa[x]);
      fa[x] = 0;
    }
  }
};

void debug() {
  static int debug=0;
  debug++;
  if (debug==1188) {
    // printf("HERE!\n");
  }
}

int main() {
#ifdef DEBUG
  freopen("104.in", "r", stdin);
  freopen("104.out", "w", stdout);
#endif

  static Splay splay;
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    int opt, x, t;
    scanf("%d %d", &opt, &x);
    switch (opt) {
    case 1:
      splay.splay(splay.ins(splay.root, x), 0);
      break;
    case 2:
      splay.del(splay.find(splay.root, x));
      break;
    case 3:
#ifdef DEBUG
      debug();
#endif
      printf("%d\n", splay.getRank(splay.find(splay.root, x)));
      break;
    case 4:
#ifdef DEBUG
      debug();
#endif
      printf("%d\n", splay.val[splay.getRankX(splay.root, x)]);
#ifdef DEBUG
      if (splay.getRank(splay.getRankX(splay.root, x))!=x) {
	// printf("ERR\n");
      }
#endif
      break;
    case 5:
#ifdef DEBUG
      debug();
#endif
      t=splay.splay(splay.ins(splay.root, x), 0);
      printf("%d\n", splay.val[splay.getPre(t)]);
      splay.del(t);
      break;
    case 6:
#ifdef DEBUG
      debug();
#endif
      t=splay.splay(splay.ins(splay.root, x), 0);
      printf("%d\n", splay.val[splay.getSuc(t)]);
      splay.del(t);
      break;
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
